package game;
import java.awt.Color;
import java.util.ArrayList;

import dataAccess.*;
import boundaryToMatador.GUI;

public class BoundaryGUI {
	
	Color[] c = {Color.LIGHT_GRAY, Color.ORANGE, Color.BLACK, Color.PINK, Color.YELLOW, Color.MAGENTA};
	
	public int getNumberOfPlayers()
	{
		return GUI.getUserInteger("How many players?", 2, 6);
	}
	
	public ArrayList<Player> addPlayers(ArrayList<Player> p, int numberOfPlayers)
	{
		
		int i=0;
		while (i < numberOfPlayers) {
			Player player = new Player();
			player.setName(GUI.getUserString("Player "+(i+1)+"'s name:"));
			
			while (true) {
			boolean out=true;
			for (int k=0; k<i; k++)
			{
				if (player.getName().equals(p.get(k).getName())) {
					player.setName(GUI.getUserString("Name is taken. Try another:"));
					out=false;
				}
			}
			if (out) break;
			}
			
			p.add(player);
			GUI.addPlayer(p.get(i).getName(), p.get(i).getAccount(), c[i]);
			GUI.setCar(1, p.get(i).getName());
			i++;
		}
		
		return p;
	}
	
	public void LoadPlayer(Player player, int i) throws DALException{
	
			GUI.addPlayer(player.getName() , player.getAccount(), c[i]);
			GUI.setCar(player.getField(), player.getName());
	}
	
	public String playerInJail(int playerOutOfJailCards) {
		if (playerOutOfJailCards==0) return GUI.getUserButtonPressed("What do you want to do?", "Pay 1000", "Roll dices");
		else return GUI.getUserButtonPressed("What do you want to do?", "Use Luckycard", "Pay 1000", "Roll dices");
	}
	
	public boolean landOnTax(int account) {
	
		GUI.showMessage("You landed on Caravan.");
		if(GUI.getUserButtonPressed("Do you want to pay 4000 or 10% of your assets","4000",Integer.toString(account/10))=="4000") {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean playAgain()
	{
		if(GUI.getUserButtonPressed("Do you want to play again?", "Yes", "No")=="Yes")
		return true;
		else return false;
	}
	
	public void startGUI()
	{
		GUI.create("fields.txt");
	}
	
	public void closeGUI()
	{
		GUI.close();
	}
	
	public boolean welcomeText()
	{
		
		return GUI.getUserLeftButtonPressed("Welcome to Monopoly", "New Game", "Load Game");
	}
	
	public String turnText(String name, int houses, boolean housesToBuy, boolean canBuildHouses, boolean propertiesToPawn, int pawnedProps)
	{
		if (housesToBuy && canBuildHouses && houses>0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Build house", "Sell house", "Pawn");
		else if (housesToBuy && canBuildHouses && houses==0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Build house", "Pawn");
		else if (!housesToBuy && canBuildHouses && houses>0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Sell house", "Pawn");
		else if (!housesToBuy && canBuildHouses && houses==0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Pawn");
		else if (housesToBuy && !canBuildHouses && houses>0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Sell house", "Pawn");
		else if (housesToBuy && !canBuildHouses && houses==0 && propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Pawn");
		else if (housesToBuy && canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Build house");
		else if (!housesToBuy && canBuildHouses && houses>0 && !propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Sell house");
		else if (!housesToBuy && canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house");
		else if (housesToBuy && !canBuildHouses && houses>0 && !propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Sell house");
		else if (housesToBuy && !canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps==0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property");
		
		else if (housesToBuy && canBuildHouses && houses==0 && propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Build house", "Pawn", "Unpawn");
		else if (!housesToBuy && canBuildHouses && houses>0 && propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Sell house", "Pawn", "Unpawn");
		else if (!housesToBuy && canBuildHouses && houses==0 && propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Pawn", "Unpawn");
		else if (housesToBuy && !canBuildHouses && houses>0 && propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Sell house", "Pawn", "Unpawn");
		else if (housesToBuy && !canBuildHouses && houses==0 && propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Pawn", "Unpawn");
		else if (housesToBuy && canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Build house", "Unpawn");
		else if (!housesToBuy && canBuildHouses && houses>0 && !propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Sell house", "Unpawn");
		else if (!housesToBuy && canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Build house", "Unpawn");
		else if (housesToBuy && !canBuildHouses && houses>0 && !propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Sell house", "Unpawn");
		else if (housesToBuy && !canBuildHouses && houses==0 && !propertiesToPawn && pawnedProps>0) return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices", "Buy elses property", "Unpawn");
		else return GUI.getUserButtonPressed(name+"'s turn. What do you want to do?", "Roll dices");
		
	}
	
	public void setBalance(String name, int account)
	{
		GUI.setBalance(name, account);
	}
    
    public void removeCar(int fieldNumber, String name) {
    	
    	GUI.removeCar(fieldNumber, name);
    }
        
	public void setCar(int fieldNumber, String name) {
	    GUI.setCar(fieldNumber, name);
	}       
	
	public void setDices(int[] dices)
	{
		GUI.setDice(dices[0], dices[1]);
	}
	
	public void landOnField(String fieldName) {
		
		GUI.showMessage("You landed on the field: " + fieldName );
	}
	
	public void landOnFieldText(String fieldName) {
		GUI.showMessage("You landed on "+fieldName);
	}
	
	public void showMessage(String msg) {
		GUI.showMessage(msg);
	}
	
	public void playerHasLost(String name, int field) {
		
		GUI.showMessage(name + " has lost. Rest of the players keeps playing.");
		GUI.removeCar(field, name);
		GUI.showMessage(name + "'s properties is now ownable again.");
	}
	
	public void playerHasWon(String name) {
		
		GUI.showMessage("Player: "+name+" has won!! Congratulations!");
	}
	
	public String haveToGetOutOfJail(int outOfJailCards) {
		if (outOfJailCards>0) return GUI.getUserButtonPressed("You have been in jail for 3 rounds. You have to get out.", "Use Luckycard", "Pay 1000");
		else return GUI.getUserButtonPressed("You have been in jail for 3 rounds. You have to get out.", "Pay 1000");
	}
	
	public boolean yesOrNo(String msg, String yesButton, String noButton) {
		return GUI.getUserLeftButtonPressed(msg, yesButton, noButton);
	}
	
	public void setOwner(int fieldNumber, String name) {
		GUI.setOwner(fieldNumber, name);
	}
	
	public void removeOwner(int fieldNumber) {
		GUI.removeOwner(fieldNumber);
	}
	
	public String brokeOptions(int buildings, boolean canPawn) {
		if (buildings>0 && canPawn) return GUI.getUserButtonPressed("What do you want to do?", "Pawn", "Sell buildings", "Nothing");
		else if (buildings==0 && canPawn) return GUI.getUserButtonPressed("What do you want to do?", "Pawn", "Nothing");
		else if (buildings>0 && !canPawn) return GUI.getUserButtonPressed("What do you want to do?", "Sell buildings", "Nothing");
		else return GUI.getUserButtonPressed("What do you want to do?", "Nothing");
	}
	
	public int getUserBid() {
		return GUI.getUserInteger("Enter your bid for this property:");
	}
	
	public boolean acceptBid(int bid) {
		return GUI.getUserLeftButtonPressed("Do you accept the bid of "+bid+"?", "Yes", "No");
	}
	
	public void addHotel(int fieldnumber) {
		GUI.setHotel(fieldnumber, true);
	}
	
	public void setHouses(int fieldnumber, int houses) {
		GUI.setHouses(fieldnumber, houses);
	}
	
	public int getFieldnumber(String msg) {
		return GUI.getUserInteger(msg, 1, 40);
	}
}
